function allCovs = GenerateCovariates_LongTerm(saveBool, sequenceFile, stimDur)
%
%
%   _______________________________________________
%   by Marcelo G Mattar (07/24/2012)
%   mattar@sas.upenn.edu


%% CHECK INPUTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if nargin < 3
    stimDur = 3.200;
end

if nargin < 2
    sequenceFile = '/Users/marcelomattar/Dropbox/Marcelo/UPenn/Documents/Projects/AdaptID/Experiment/config/seqs/LongTermfMRI.txt';
end

if nargin < 1
    saveBool = 0;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Load the sequence
thisSeq = double(importdata(sequenceFile));

% Initialize covariates
oldFace = thisSeq==1;
newFace = thisSeq==2;
targetFace = thisSeq==-1;

% Save covariates to external files
if saveBool
    if ~exist('./covariates/', 'dir')
        mkdir('./covariates/');
    end
    if ~exist('./covariates/longTerm/', 'dir')
        mkdir('./covariates/longTerm/');
    end
    i = max(strfind(sequenceFile, '/'));
    j = max(strfind(sequenceFile, '.'));
    sequenceName = sequenceFile((i+1):(j-1));
    
    oldFace_fileID = fopen('./covariates/longTerm/oldFace.txt','w');
    newFace_fileID = fopen('./covariates/longTerm/newFace.txt','w');
    targetFace_fileID = fopen('./covariates/longTerm/targetFace.txt','w');
    
    for trial=1:(length(thisSeq)-1)
        if oldFace(trial) ~= 0
            fprintf(oldFace_fileID,'%f\t%f\t%f\n',stimDur*(trial-1),stimDur,oldFace(trial));
        end
        if newFace(trial) ~= 0
            fprintf(newFace_fileID,'%f\t%f\t%f\n',stimDur*(trial-1),stimDur,newFace(trial));
        end
        if targetFace(trial) ~= 0
            fprintf(targetFace_fileID,'%f\t%f\t%f\n',stimDur*(trial-1),stimDur,targetFace(trial));
        end
    end
    if oldFace(trial) ~= 0
        fprintf(oldFace_fileID,'%f\t%f\t%f',stimDur*(length(thisSeq)-1),stimDur,oldFace(end));
    end
    if newFace(trial) ~= 0
        fprintf(newFace_fileID,'%f\t%f\t%f',stimDur*(length(thisSeq)-1),stimDur,newFace(end));
    end
    if targetFace(trial) ~= 0
        fprintf(targetFace_fileID,'%f\t%f\t%f',stimDur*(length(thisSeq)-1),stimDur,targetFace(end));
    end
    
end

allCovs.oldFace = oldFace;
allCovs.newFace = newFace;
allCovs.targetFace = targetFace;

